package com.jisf.driver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jisf.driver.entity.po.RoleMenu;
import com.jisf.driver.entity.vo.AllotMenusVo;
import com.jisf.driver.entity.vo.MenuListVo;
import com.jisf.driver.exception.SystemException;
import com.jisf.driver.mapper.RoleMenuMapper;
import com.jisf.driver.response.IErrorCode;
import com.jisf.driver.response.ResultCode;
import com.jisf.driver.service.IMenuService;
import com.jisf.driver.service.IRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jisf
 * @since 2022-04-08
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private IMenuService iMenuService;

    /**
     * 删除角色的权限
     * @param roleId
     * @param menuId
     * @return
     */
    @Override
    public IErrorCode deleteRoleMenu(Integer roleId, Integer menuId){
       QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("role_id", roleId).eq("menu_id", menuId);
        int delete = roleMenuMapper.delete(queryWrapper);
        return delete == 0 ? ResultCode.ROLE_MENU_DELETE_FAILED : ResultCode.ROLE_MENU_DELETE_SUCCESS;
    }

    /**
     * 根据角色id获取对应权限
     * @param roleId
     * @return 角色权限更新后的权限数据
     */
    public List<MenuListVo> findRoleMenus(Integer roleId){
        List<MenuListVo> MenuListVos = roleMenuMapper.findMenuListVoByRoleId(roleId);
        List<MenuListVo> roleMenus = iMenuService.organizeMenu(MenuListVos);
        return roleMenus;
    }

    /**
     * 为角色分配权限
     * @param allotMenusVo
     * @return
     */
    @Override
    public IErrorCode allotMenus(AllotMenusVo allotMenusVo) {
        try {
            Integer roleId = allotMenusVo.getRoleId();

            List<Integer> menusIdVo = allotMenusVo.getMenusId();
            List<Integer> menusIdPo = roleMenuMapper.selectMenusByRoleId(roleId);

            // 计算需要删除的权限和需要新增的权限
            calculateInsertDelete(menusIdVo, menusIdPo);

            // 删除权限
            int delete = 0;
            if (menusIdPo.size() != 0) {
                delete = roleMenuMapper.deleteRoleMenus(roleId, menusIdPo);
            }
            // 新增权限
            int insert = 0;
            if (menusIdVo.size() != 0) {
                insert = roleMenuMapper.insertRoleMenus(roleId, menusIdVo);
            }

            return delete+insert == 0 ? ResultCode.ROLE_MENU_ALLOT_FAILED : ResultCode.ROLE_MENU_ALLOT_SUCCESS;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 计算需要删除的角色/权限和需要新增的角色/权限
     * @param idsVo
     * @param idsPo
     */
    public void calculateInsertDelete(List<Integer> idsVo, List<Integer> idsPo){
        for (int i = 0; i < idsVo.size(); i++) {
            for (int j = 0; j < idsPo.size(); j++) {
                if (idsVo.get(i).equals(idsPo.get(j))) {
                    idsVo.remove(i--);
                    idsPo.remove(j);
                    break;
                }
            }
        }
    }
}
